﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;


namespace OntoWPFSilverlight.Widget
{
    [OntoWPFSilverlight.Annotation.Widget("ComboBox")]
    public class ComboBox : WidgetCanvas, Interfaces.IWidgetWithItems 
    {
        private System.Windows.Controls.ComboBox myComboBox = null;
        private TextBlock label = null;

        public ComboBox() : base(false)
        {
            Build();
        }

        private void Build()
        {
            //StackPanel stackPanel = new StackPanel();
            myComboBox = new System.Windows.Controls.ComboBox();
            label = new TextBlock();
            label.TextAlignment = TextAlignment.Center;
            DockPanel.SetDock(label, Dock.Top);
            DockPanel.SetDock(myComboBox, Dock.Top);
            this.Children.Add(label);
            this.Children.Add(myComboBox);
            //this.Width = this.WidgetSize.Width;
            //this.Height = this.WidgetSize.Height;
            
        }

        //public void AddItem(IEnumerable<XElement> nodes)
        //{
        //    foreach (XElement node in nodes) 
        //    {
        //        myComboBox.Items.Add(node.Attribute("Name").Value);
        //    }
        //}



        //public void AddLabel(string text) 
        //{
        //    label.Text = text;
        //}

        public override string Text
        {
            get
            {
                return this.label.Text;
            }
            set
            {
                this.label.Text = value;
            }
        }


        #region IWidgetWithItems Membri di

        OntoWPFSilverlight.Node ontologyNode;

        public OntoWPFSilverlight.Node Node
        {
            get
            {
                return ontologyNode;
            }
            set
            {
                ontologyNode = value;
            }
        }

        OntoWPFSilverlight.Xml.OntologiesNodes ontologiesNodes = null;
        public OntoWPFSilverlight.Xml.OntologiesNodes OntologiesNodes
        {
            get
            {
                return ontologiesNodes;
            }
            set
            {
                this.ontologiesNodes = value;
            }
        }
        
        public void update(List<Node> nodes)
        {
            
            foreach (Node _node in nodes)
            {
                myComboBox.Items.Add(_node.Alias);
                try
                {
                    myComboBox.FontSize = Double.Parse(this.WidgetFont.Size);
                }
                catch (Exception)
                {

                }
                //try{
                //    myComboBox.FontFamily = new FontFamily(this.WidgetFont.Name);
                //}
                //catch{}
            }
        }

        //public override string  
        public void update()
        {
            update(this.ontologiesNodes.Nodes);
        }

        #endregion
    }
}
